#include <htc.h>
#include"SPI.h"

void spi_init()
{
	SS = 1;
	SCK = 1;
	SS = 1;
	// I/O init
	TRISE0 = 0;	// SCK OUT
	TRISE1 = 0;	// SDO
	TRISE2 = 1;	// SDI
	TRISA5 = 0; // Slave Select
	
}

void spi_write(char data)
{
	//CKP = 1; CKE=0;
	char count;
	for (count=0; count<8; count++)
	{
		SCK = 0;
		SDO = (~data>>count)&0x01;
		SCK = 1;
	}
}

char spi_read()
{
	//CKP = 1; CKE=0;
	char data = 0, count;
	SS = 0;	// Select Slave
	for (count=0; count<8; count++)
	{
		SCK = 0;
		data |= SDI << count;

		SCK = 1;
	}
	SS = 1;	// Deselect Slave
	return(data);
	
}
